﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Printing;
using System.Drawing;
using DataUpload;
using Xceed.Wpf.Toolkit;
using Logistics.Configuration;
using System.IO;
using log4net;

namespace Logistics.Print
{
    class PrinterUtils
    {
        public const String PRINTER_TEST_BILL_ID = "__#PRINTER_TEST_BILL_ID#";

        private static ILog Log = LogManager.GetLogger(typeof(PrinterUtils).FullName);
        private static String TemplatePath
        {
            get { return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Print_Templates\\BillTemplate.xls"); }
        }
        private static String TagTemplatePath
        {
            get { return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Print_Templates\\BillTagTemplate.xls"); }
        }

        #region 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="BillID"></param>
        public static void PrintWayBill(String BillID)
        {
            String xmlFile = GlobalObjects.ConfigFileLocation;
            string _ticketPrint = XmlFiles.Read(xmlFile, "/system/print/ticket", "");//打印机名称
            if (string.IsNullOrEmpty(_ticketPrint))
            {
                MessageBox.Show("票据打印机设置不正常，请您检测并设置票据打印机！");
                return;
            }
            PrintWayBill(BillID, _ticketPrint);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="BillID"></param>
        /// <param name="printerName"></param>
        public static void PrintWayBill(String BillID,String printerName)
        {
            Log.Debug("准备生成票据打印文件");
            String filePath = PrintTemplate.GenerateBillFileForPrint(BillID, TemplatePath);
            Log.Debug("成功生成票据打印文件:" + filePath);
            PrintTemplate.PrintExcel(filePath, printerName, 1, null);
        }
        #endregion

        #region 
        public static void PrintWayBillTags(String BillID, int count)
        {
            if (count > 0)
            {
                String xmlFile = GlobalObjects.ConfigFileLocation;
                string _tagsPrint = XmlFiles.Read(xmlFile, "/system/print/tags", "");//打印机名称
                if (string.IsNullOrEmpty(_tagsPrint))
                {
                    MessageBox.Show("标签打印机设置不正常，请您检测并设置标签打印机！");
                    return;
                }
                PrintWayBillTags(BillID, count, _tagsPrint);
            }
        }
        public static void PrintWayBillTags(String BillID, int count,string printerName)
        {
            if (count > 0)
            {
                Log.Debug("准备生成标签打印文件");
                String filePath = PrintTemplate.GenerateBillFileForPrint(BillID, TagTemplatePath);
                Log.Debug("成功生成标签打印文件:" + filePath);
                PrintTemplate.PrintExcel(filePath, printerName, count, null);
            }
        }
        #endregion
    }
}
